package gatewayservice.gatewayservice.controller;

import gatewayservice.gatewayservice.UserDto;
import gatewayservice.gatewayservice.mapper.UserMapper;
import gatewayservice.gatewayservice.po.User;
import gatewayservice.gatewayservice.utils.JwtUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;

@AllArgsConstructor
@RestController
@Slf4j
public class UserController {

    private final UserMapper userMapper;

    @PostMapping("/login")
    public String userLogin(@RequestBody UserDto userDto){
        User result = userMapper.login(userDto);
        if(result==null){
            return "用户名或密码错误";
        }
        else{
            Map<String,Object> claims = new HashMap<>();
            claims.put("id",result.getId());
            claims.put("username",result.getUsername());
            claims.put("password",result.getPassword());

            return JwtUtils.generateJwt("yunyuansheng",3600*1000,claims);
        }
    }
}
